import { RouteConfig } from "vue-router";
import { IViewItem } from "@/model/frame/IViewItem";
import FrameConfig from "@/model/frame/FrameConfig";

export class RoutesTool {
  public static maintenance(component: string): RouteConfig {
    return this.route("Maintenance", component, "/Maintenance");
  }

  public static comingSoon(component: string): RouteConfig {
    return this.route("ComingSoon", component, "/ComingSoon");
  }

  public static login(component: string): RouteConfig {
    return this.route("Login", component, "/Login");
  }

  public static notFound(component: string): RouteConfig {
    return this.route("404", component, "/404");
  }

  public static noPermission(component: string): RouteConfig {
    return this.route("403", component, "/403");
  }

  public static error(component: string): RouteConfig {
    return this.route("500", component, "/500");
  }

  public static layout(
    children: RouteConfig[],
    path = "",
    componentPath = FrameConfig.APP_LAYOUT_PATH
  ): RouteConfig {
    return {
      children,
      component: () => import(`@/ui/components/${componentPath}`),
      path
    };
  }

  public static viewItemRoutes(viewItems: IViewItem[]): RouteConfig[] {
    const routes: RouteConfig[] = [];
    viewItems.map(viewItem => {
      if (
        typeof viewItem.routePath != "undefined" &&
        typeof viewItem.filePath != "undefined"
      ) {
        routes.push(this.viewItemRoute(viewItem));
      }
      if (viewItem.items) {
        routes.push(...this.viewItemRoutes(viewItem.items));
      }
    });
    return routes;
  }

  public static viewItemRoute(viewItem: IViewItem): RouteConfig {
    if (!viewItem.filePath) {
      throw new Error("viewItem 没有FilePath");
    }

    return RoutesTool.route(
      viewItem.name,
      viewItem.filePath!,
      viewItem.routePath!
    );
  }

  public static route(
    name: string,
    component: string,
    path: string
  ): RouteConfig {
    return {
      name: name,
      component: () => import(`@/ui/views/${component}`),
      path: path
    };
  }
}
